extern "C" { 
    #include "apue.h" 
}   

#include <stdio.h>
#include <stdlib.h>


#include <sys/types.h>
#include <unistd.h>





void together(void)
{
    int i=0;
    printf("-------parrent_exit_first-------\n");
    printf("I am parrent\n");
    pid_t pid; 
    if((pid=fork())==0) //child
    {
        while(1)
        {
            printf("%4d: I am child,pid=%d,ppid=%d\n",i++,getpid(),getppid());
            sleep(5);
        }
    }
    else
    {
        while(1)
        {
            printf("%4d: I am parrent_exit_first,pid=%d,ppid=%d\n",i++,getpid(),getppid());
            sleep(5);
        }
    }

}



void parrent_exit_first(void)
{
    int i=0;
    printf("-------parrent_exit_first-------\n");
    printf("I am parrent\n");
    pid_t pid; 
    if((pid=fork())==0) //child
    {
        while(1)
        {
            printf("%4d: I am child,pid=%d,ppid=%d\n",i++,getpid(),getppid());
            sleep(3);
        }
    }
    else
    {
        while(1)
        {
            static int num=0;
            if(num++ >10)
            {
                printf("parrent_exit_first,pid=%d,ppid=%d\n",getpid(),getppid());
                return ;
            }
            printf("%4d: I am parrent_exit_first,pid=%d,ppid=%d\n",i++,getpid(),getppid());
            sleep(3);
        }
    }

}

void child_exit_first(void)
{
    int i=0;
    printf("-------child_exit_first-------\n");
    printf("I am parrent\n");
    pid_t pid; 
    if((pid=fork())==0) //child
    {
        while(1)
        {
            static int num=0;
            if(num++ >10)
            {
                printf("childt_exit_first,pid=%d,ppid=%d\n",getpid(),getppid());
                return ;
            }
            printf("%4d: I am child,pid=%d,ppid=%d\n",i++,getpid(),getppid());
            sleep(3);
        }
    }
    else
    {
        while(1)
        {
            printf("%4d: I am parrent_exit_first,pid=%d,ppid=%d\n",i++,getpid(),getppid());
            sleep(3);
        }
    }

}



int main(int argc, char const *argv[])
{

    ///together();
    //parrent_exit_first();
    child_exit_first();
    return 1;
}
